package com.dclib.dctracer;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	static final String DB_NAME = "dctracer";
	static final String TABLE_NAME = "tb_options";
	static final int DB_VERSION = 1;
	private static final String LOG_TAG = "DBHelper:";
	private static final String KEY_ROWID = "id";
	static final String KEY_VALUE = "value";
	static final String KEY_OPTIONS = "options";

	public SQLiteDatabase database;

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		Log.d(LOG_TAG, "--- onCreate database, Create table " + TABLE_NAME
				+ " ---");

		db.execSQL("create table " + TABLE_NAME + " ( " + KEY_ROWID
				+ " integer primary key autoincrement, " + KEY_OPTIONS
				+ " TEXT, " + KEY_VALUE + " TEXT)");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public void reiniDB(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
		onCreate(db);
	}

	public String getData() {
		String[] columns = new String[] { "options", "value" };
		Cursor c = database.query(TABLE_NAME, columns, null, null, null, null,
				KEY_ROWID);
		String result = "";

		int iRow = c.getColumnIndex(KEY_ROWID);
		int ivalue = c.getColumnIndex(KEY_VALUE);
		int ioptions = c.getColumnIndex(KEY_OPTIONS);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			result = result + c.getString(iRow) + " " + c.getString(ivalue)
					+ " " + c.getString(ioptions)+"\n";

		}
		return result;
	}
}
